package com.anti.modular.owner.ownerleasebilladjust.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.anti.jdbc.DBUtils;
import com.anti.jdbc.support.page.PageParam;
import com.anti.jdbc.support.page.PageResult;
import com.anti.commom.core.exception.BusinessException;
import com.anti.modular.owner.ownerleasebilladjust.entity.OwnerLeaseBillAdjust;
import com.anti.modular.owner.ownerleasebilladjust.enums.OwnerLeaseBillAdjustExceptionEnum;
import com.anti.modular.owner.ownerleasebilladjust.param.OwnerLeaseBillAdjustParam;
import com.anti.modular.owner.ownerleasebilladjust.service.OwnerLeaseBillAdjustService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 业主账单表调整表service接口实现类
 *
 * @author anti
 * @date 2021-05-20 14:27:27
 */
@Service
public class OwnerLeaseBillAdjustServiceImpl implements OwnerLeaseBillAdjustService {
    @Resource
    DBUtils dbUtils;

    @Override
    public OwnerLeaseBillAdjust getById(OwnerLeaseBillAdjustParam ownerLeaseBillAdjustParam){
        String sql="select * from owner_lease_bill_adjust where id =? ";
        return dbUtils.get(sql,OwnerLeaseBillAdjust.class,ownerLeaseBillAdjustParam.getId());
    }

    @Override
    public OwnerLeaseBillAdjust getByBillId(Long id) {
        String sql="select * from owner_lease_bill_adjust where lease_bill_id =? ";
        return dbUtils.getFirst(sql,OwnerLeaseBillAdjust.class,id);
    }

    @Override
    public PageResult page(PageParam pageParam,OwnerLeaseBillAdjustParam ownerLeaseBillAdjustParam) {
        StringBuilder sb=new StringBuilder("select * from owner_lease_bill_adjust where 1=1 ");
        ArrayList<Object> param = CollectionUtil.newArrayList();
        if (ObjectUtil.isNotNull(ownerLeaseBillAdjustParam)) {
            // 根据业主账单主键 查询
            if (ObjectUtil.isNotEmpty(ownerLeaseBillAdjustParam.getLeaseBillId())) {
               sb.append(" and lease_bill_id = ?");
               param.add(ownerLeaseBillAdjustParam.getLeaseBillId());
            }
            // 根据租金调整(字典 1 涨  2 降) 查询
            if (ObjectUtil.isNotEmpty(ownerLeaseBillAdjustParam.getAdjustType())) {
               sb.append(" and adjust_type = ?");
               param.add(ownerLeaseBillAdjustParam.getAdjustType());
            }
            // 根据租金方式(字典 1 金额  2 百分比) 查询
            if (ObjectUtil.isNotEmpty(ownerLeaseBillAdjustParam.getAdjustStyle())) {
               sb.append(" and adjust_style = ?");
               param.add(ownerLeaseBillAdjustParam.getAdjustStyle());
            }
            // 根据调整值 查询
            if (ObjectUtil.isNotEmpty(ownerLeaseBillAdjustParam.getChangeMoney())) {
               sb.append(" and change_money = ?");
               param.add(ownerLeaseBillAdjustParam.getChangeMoney());
            }
            // 根据调整为 查询
            if (ObjectUtil.isNotEmpty(ownerLeaseBillAdjustParam.getRent())) {
               sb.append(" and rent = ?");
               param.add(ownerLeaseBillAdjustParam.getRent());
            }
            // 根据状态（字典 0正常 1注銷 2刪除） 查询
            if (ObjectUtil.isNotEmpty(ownerLeaseBillAdjustParam.getStatus())) {
               sb.append(" and status = ?");
               param.add(ownerLeaseBillAdjustParam.getStatus());
            }
        }
        return dbUtils.query(pageParam,sb.toString(),param.toArray());
    }

    @Override
    public List<OwnerLeaseBillAdjust> list(OwnerLeaseBillAdjustParam ownerLeaseBillAdjustParam) {
        String sql="";
        return dbUtils.query(sql,OwnerLeaseBillAdjust.class);
    }

    @Override
    public void add(OwnerLeaseBillAdjustParam ownerLeaseBillAdjustParam) {
        OwnerLeaseBillAdjust ownerLeaseBillAdjust = new OwnerLeaseBillAdjust();
        BeanUtil.copyProperties(ownerLeaseBillAdjustParam, ownerLeaseBillAdjust);
        dbUtils.insert(ownerLeaseBillAdjust);
    }

    @Override
    public void addEntity(OwnerLeaseBillAdjust OwnerLeaseBillAdjust) {
        dbUtils.insert(OwnerLeaseBillAdjust);
    }

    public void delete(OwnerLeaseBillAdjustParam ownerLeaseBillAdjustParam) {
        String sql="delete  from owner_lease_bill_adjust where id =? ";
        dbUtils.execSql(sql,ownerLeaseBillAdjustParam.getId());
    }

    @Override
    public void deleteSuf(Long leaseId, Long nowBillId) {
        String sql="delete  from owner_lease_bill_adjust where lease_bill_id in (select id from owner_lease_bill where owner_lease_id=? and id>=?) ";
        dbUtils.execSql(sql,leaseId,nowBillId);
    }

    public void edit(OwnerLeaseBillAdjustParam ownerLeaseBillAdjustParam) {
        OwnerLeaseBillAdjust ownerLeaseBillAdjust = this.queryOwnerLeaseBillAdjust(ownerLeaseBillAdjustParam);
        BeanUtil.copyProperties(ownerLeaseBillAdjustParam, ownerLeaseBillAdjust);
        dbUtils.update(ownerLeaseBillAdjust);
    }

    @Override
    public OwnerLeaseBillAdjust detail(OwnerLeaseBillAdjustParam ownerLeaseBillAdjustParam) {
        return this.queryOwnerLeaseBillAdjust(ownerLeaseBillAdjustParam);
    }

    /**
     * 获取业主账单表调整表
     *
     * @author anti
     * @date 2021-05-20 14:27:27
     */
    private OwnerLeaseBillAdjust queryOwnerLeaseBillAdjust(OwnerLeaseBillAdjustParam ownerLeaseBillAdjustParam) {
        OwnerLeaseBillAdjust ownerLeaseBillAdjust = this.getById(ownerLeaseBillAdjustParam);
        if (ObjectUtil.isNull(ownerLeaseBillAdjust)) {
            throw new BusinessException(OwnerLeaseBillAdjustExceptionEnum.NOT_EXIST);
        }
        return ownerLeaseBillAdjust;
    }


}
